#include"Heap.h"
void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
void HeapInit(HP* hp)
{
	assert(hp);
	hp->arr = NULL;
	hp->Capacity = hp->top = 0;
}
void HeapPush(HP* hp, HPDataType val)
{
	if (hp->Capacity == hp->top)
	{
		int NewCapacity = hp->Capacity == 0 ? 4 : 2 * hp->Capacity;
		HPDataType* tmp = (HPDataType*)malloc(sizeof(HPDataType) * NewCapacity);
		if (tmp == NULL)
		{
			perror("malloc failed");
			exit(1);
		}
		hp->arr = tmp;
		hp->Capacity = NewCapacity;
	}
	hp->arr[hp->top++] = val;
}
void HeapDestroy(HP* hp)
{
	if (hp->arr)
	{
		free(hp->arr);
		hp->top = hp->Capacity = 0;
	}
}
void AdjustDown(HPDataType* arr, int parent, int n)
{
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && arr[child + 1] < arr[child]) 
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
		{
			break;
		}
	}
}